"""
Embedded Python Blocks:
Each time this file is saved, GRC will instantiate the first class it finds
to get ports and parameters of your block. All parameters must have default values!
"""
import numpy as np
from gnuradio import gr
import cv2

class blk(gr.sync_block):
    """图像处理模块：将灰度图像行转换为复数信号"""
    
    def __init__(self, 
                image_path=r'E:\1.study\work\SDR\GNUwork\waterfall_picture\picture.jpg',
                length=1024):
        
        gr.sync_block.__init__(
            self,
            name='image processing module',
            in_sig=None,
            out_sig=[(np.complex64, length)]
        )

        self.image_path = image_path
        img = cv2.imread(self.image_path)
        self.img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        self.row, self.col = self.img_gray.shape

        self.index = 0
        self.fft_length = length

    def work(self, input_items, output_items):
        '''
        输出结构说明：
        output_items[0] 形状为 [可用输出项数, fft长度]
        '''

        out = output_items[0]
        num_outputs = out.shape[0]
        

        for i in range(num_outputs):
            out[i] = np.zeros(self.fft_length, dtype=np.complex64)

            out[i, :self.col] = (self.img_gray[self.index] / 255.0).astype(np.complex64)
            self.index = (self.index + 1) % self.row
        return num_outputs